<!-- 单个玩家调控 -->
<template>
  <el-dialog v-if="props.show" :modelValue="props.show" title="" width="60%" top="8vh" :close-on-click-modal="false"
    :close-on-press-escape="false" :destroy-on-close="true" @close="handleClose">
    <template #header>
      <p class="box-title marginB5">玩家【 <span class="mustPass">{{ props.checkedItem?.account3d_id }}</span> 】{{
        props.checkedItem?.account3d }}</p>
    </template>
    <div v-loading="indexStore.moduleLoading" class="dialogMax" style="overflow: hidden;">
      <div class="padding10">
        <form-item title="调控游戏" :pass="true" value="game_id">
          <el-select class="imWidth100" v-model="data.userControl.game_ids" placeholder="请选择游戏" filterable multiple
            @change="chooseGameType">
            <el-option label="全部" :value="-1" />
            <el-option v-for="(item, index) in data.gameList" :key="index"
              :label="'【' + item.value + '】' + item.label" :value="item.value"/>
          </el-select>
        </form-item>
        <form-item title="RTP" :pass="true" value="rtp">
          <el-select class="width150" v-model="data.userControl.rtp" placeholder="请选择RTP" filterable>
            <el-option v-show="item && item==200?operatorTypeShow:true" v-for="(item, index) in data.rtpList" :key="index" :label="item+'%'" :value="item" />
          </el-select>
          <el-button class="marginL20" type="success" :disabled="data.userControl.game_ids.length?false:true" @click="addControl">新增调控</el-button>
        </form-item>
      </div>
      <div class="marginT10">
        <div class="flex marginB10">
          <el-button class="marginR20" type="danger" :disabled="data.chooseControlList.length?false:true" @click="data.isControlShow = true">批量取消</el-button>
          自动解除控制rtp： <el-input-number v-model="data.auto_cancel_rtp" :min="0" :max="99999999999999999999" :precision="0"
          class="width150" placeholder="请输入RTP设定值" clearable :controls="false" />%
        </div>
        <el-table v-loading="indexStore.moduleLoading" :data="data.userGameControlList" border style="height: 450px;" @select-all="chooseControlRtp" @select="chooseControlRtp" show-overflow-tooltip>
          <el-table-column type="selection" width="45" />
          <el-table-column prop="game_id" label="游戏" #default="scope" width="200">
            <GameHeaderInfo class="flexCenter" :game_id="scope.row.game_id" />
          </el-table-column>
          <el-table-column prop="rtp" label="RTP" #default="scope"  width="90">
            {{ BigHundred(scope.row.rtp) }}%
          </el-table-column>
          <el-table-column prop="create_at" label="调控时间" #default="scope" width="200">
            {{ setTimestampTime(scope.row.create_at) }}
          </el-table-column>
          <el-table-column prop="control_that_rtp" label="控制时的RTP" #default="scope">
            {{ scope.row.control_that_rtp?BigHundred(scope.row.control_that_rtp)+'%':'-' }}
          </el-table-column>
          <el-table-column prop="auto_cancel_rtp" label="自动解除RTP" #default="scope">
            {{ scope.row.auto_cancel_rtp?BigHundred(scope.row.auto_cancel_rtp)+'%':'-' }}
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="120">
            <template #default="scope">
              <el-button :type="scope.row.user_rtp_game_info ? 'success' : 'danger'" v-permiss="'/system/destroyManager'"
                size="small" @click="delControl(scope.row)">
                取消调控
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </el-dialog>
  <!-- 批量调控 -->
  <el-dialog :modelValue="data.isControlShow" title="批量调控" width="45%" top="25vh" :close-on-click-modal="false"
    :close-on-press-escape="false" @close="data.isControlShow = false">
    <template #header>
      <p class="box-title marginB5">取消玩家【 <span class="mustPass">{{ props.checkedItem?.account3d_id }}</span> 】{{
        props.checkedItem?.account3d }} 对下列游戏调控</p>
    </template>
    <div class="flexWrap">
      <span class="margin5" v-for="(item,index) in data.chooseControlList" :key="index">
        <GameHeaderInfo class="flexCenter" :game_id="item.game_id" />
      </span>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="info" @click="data.isControlShow = false">取 消</el-button>
        <el-button type="primary" @click="delAllControl">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import GameHeaderInfo from '@/components/FormItem/GameHeaderInfo.vue'
import { selectToLabel, throttle } from '@/utils/index'
import { BigHundred, BigTimeHundred } from '@/utils/math'
import { setTimestampTime } from '@/utils/time'
import { getControlUser, changeAllControlUser, changeControlUser } from '@/api/player'
import { getOperatorTiger } from '@/api/game'

import { useStore } from '@/stores/index'
const indexStore = useStore()

const props = defineProps({
  show: {
    type: Boolean,
    default: () => {
      return false
    }
  },
  checkedItem: {
    type: Object,
    default: () => {
      return {} as any
    }
  },
})

const operatorTypeShow = (!indexStore.is_general_agent || indexStore.userInfo.operator_type) ? true:false
const data = reactive({
  rtpList: [] as any[],
  userControl: {
    agent_id: 0,
    game_ids: [],
    user_ids: [],
    rtp: 70,
    switch: 1,
  },
  auto_cancel_rtp: 0,
  userGameControlList:[] as any[],
  chooseControlList:[] as any[],
  isControlShow: false,
  gameList: [] as any
})

watch(
  () => [props.checkedItem,props.show],
  async (value) => {
    if (value[0] && value[1]) {
      getGameRtp()
      getUserRtp(value[0])
      data.gameList = []
      indexStore.gameArray.forEach((item:any)=> {
        if(item.type==1){
          data.gameList.push(item)
        }
      })
    }
  }
)

// 游戏RTP
const getGameRtp = async () => {
  let res = await getOperatorTiger({
    agent_id: 0, game_id: 39
  })
  data.rtpList = res.data.rtpList
}

// 获取玩家RTP调控
const getUserRtp = async (row: any) => {
  data.userControl.agent_id = row.agent_id
  data.userControl.user_ids = [row.account3d_id] as any
  indexStore.setPageLoading(true)
  let res = await getControlUser({page:1,size:300,search_type: 1,search_value: row.account3d_id,agent_id:row.agent_id})
  data.userGameControlList = res.data.list
}
const addControl = throttle(async () => {
  let query = JSON.parse(JSON.stringify(data.userControl));
  query.switch = 2
  query.rtp = BigTimeHundred(query.rtp)
  query.auto_cancel_rtp = BigTimeHundred(data.auto_cancel_rtp)
  // 选择全部
  const allGame = query.game_ids.find((item: number) => item == -1)
  if (allGame) {
    query.game_ids = []
    data.gameList.forEach((item: any) => {
      query.game_ids.push(item.value)
    })
  }
  indexStore.setModuleLoading(true)
  await changeControlUser(query)
  ElMessage.success('新增RTP调控成功')
  onHideDetails()
}, 5000)

// 成功取消RTP调控调控
const chooseControlRtp = (val: any) => {
  data.chooseControlList = val
}
const delControl = async (row:any) => {
  let query = JSON.parse(JSON.stringify(data.userControl)),message='';
  query.switch = 1
  query.rtp = 0
  query.game_ids = [row.game_id]
  message = `【${row.game_id}】${selectToLabel(indexStore.gameArray, row.game_id)}`
  ElMessageBox.confirm('确定取消( '+message+' )RTP调控', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  }).then(async () => {
    indexStore.setModuleLoading(true)
    await changeControlUser(query)
    onHideDetails()
    ElMessage.success('成功取消RTP调控')
  }).catch(() => {})
}

// 批量成功取消RTP调控调控
const delAllControl =  throttle(async () => {
  let query = {} as any;
  query.user_ids = []
  query.game_ids = [0]
  data.chooseControlList.forEach((item: any) => {
    query.user_ids.push(item.agent_id + '_' + item.account3d_id +'_' + item.game_id)
  })
  query.switch = 1
  query.rtp = 0
  if(!query.user_ids.length){
    return ElMessage.error('请选择有效控制的玩家')
  }
  await changeAllControlUser(query)
  ElMessage.success('成功取消RTP调控')
  onHideDetails()
},5000)

// 选择全部游戏
const chooseGameType = async (row: any) => {
  const all = row.find((item: number) => item == -1)
  if (all) {
    data.userControl.game_ids = [-1] as any
  } else {
    data.userControl.game_ids = row
  }
}

// 重置
const emit = defineEmits(['onHideDetails'])
const onHideDetails = async () => {
  data.userControl = {
    agent_id: 0,
    game_ids: [],
    user_ids: [],
    rtp: 70,
    switch: 1,
  }
  data.isControlShow = false
  data.userGameControlList = [] as any[]
  data.chooseControlList = [] as any[]
}
const handleClose = async () => {
  onHideDetails()
  emit('onHideDetails', false)
}
</script>

<style scoped lang="scss"></style>
